package game.games.rpg;

import static game.unicode.BoxDrawing.*;

public class Walls
{
    public static boolean isWallOrDoor(int x, int y, Room room)
    {
        char c = room.get(x, y).getChar();
        return c == Room.WALL || c == Room.SUPERWALL || c == Room.DOOR;
    }
    
    public static char getWall(int x, int y, Room room)
    {
        boolean isSuperWall = room.get(x, y).getChar() == Room.SUPERWALL;
        boolean isWallLeft = false;
        boolean isSuperWallLeft = false;
        if (x > 0 && isWallOrDoor(x - 1, y, room) && room.isExplored(x - 1, y))
        {
            isWallLeft = true;
            isSuperWallLeft = room.get(x - 1, y).getChar() == Room.SUPERWALL;
        }
        boolean isWallRight = false;
        boolean isSuperWallRight = false;
        if (x < room.getWidth() - 1 && isWallOrDoor(x + 1, y, room) && room.isExplored(x + 1, y))
        {
            isWallRight = true;
            isSuperWallRight = room.get(x + 1, y).getChar() == Room.SUPERWALL;
        }
        boolean isWallUp = false;
        boolean isSuperWallUp = false;
        if (y > 0 && isWallOrDoor(x, y - 1, room) && room.isExplored(x, y - 1))
        {
            isWallUp = true;
            isSuperWallUp = room.get(x, y - 1).getChar() == Room.SUPERWALL;
        }
        boolean isWallDown = false;
        boolean isSuperWallDown = false;
        if (y < room.getHeight() - 1 && isWallOrDoor(x, y + 1, room) && room.isExplored(x, y + 1))
        {
            isWallDown = true;
            isSuperWallDown = room.get(x, y + 1).getChar() == Room.SUPERWALL;
        }
        char c = 0;
        // get unicode
        if (isWallLeft && isWallRight && isWallUp && isWallDown)
        {
            c = getWallLeftRightUpDown(isSuperWall, isSuperWallLeft, isSuperWallRight, isSuperWallUp, isSuperWallDown);
        }
        else if (isWallLeft && isWallRight && isWallUp && !isWallDown)
        {
            c = getWallLeftRightUp(isSuperWall, isSuperWallLeft, isSuperWallRight, isSuperWallUp);
        }
        else if (isWallLeft && isWallRight && !isWallUp && isWallDown)
        {
            c = getWallLeftRightDown(isSuperWall, isSuperWallLeft, isSuperWallRight, isSuperWallDown);
        }
        else if (isWallLeft && isWallRight && !isWallUp && !isWallDown)
        {
            c = getWallLeftRight(isSuperWall, isSuperWallLeft, isSuperWallRight);
        }
        else if (isWallLeft && !isWallRight && isWallUp && isWallDown)
        {
            c = getWallLeftUpDown(isSuperWall, isSuperWallLeft , isSuperWallUp , isSuperWallDown);
        }
        else if (isWallLeft && !isWallRight && isWallUp && !isWallDown)
        {
            c = getWallLeftUp(isSuperWall, isSuperWallLeft, isSuperWallUp);
        }
        else if (isWallLeft && !isWallRight && !isWallUp && isWallDown)
        {
            c = getWallLeftDown(isSuperWall, isSuperWallLeft, isSuperWallDown);
        }
        else if (isWallLeft && !isWallRight && !isWallUp && !isWallDown)
        {
            c = getWallLeft(isSuperWall, isSuperWallLeft);
        }
        else if (!isWallLeft && isWallRight && isWallUp && isWallDown)
        {
            c = getWallRightUpDown(isSuperWall, isSuperWallRight, isSuperWallUp, isSuperWallDown);
        }
        else if (!isWallLeft && isWallRight && isWallUp && !isWallDown)
        {
            c = getWallRightUp(isSuperWall, isSuperWallRight, isSuperWallUp);
        }
        else if (!isWallLeft && isWallRight && !isWallUp && isWallDown)
        {
            c = getWallRightDown(isSuperWall, isSuperWallRight, isSuperWallDown);
        }
        else if (!isWallLeft && isWallRight && !isWallUp && !isWallDown)
        {
            c = getWallRight(isSuperWall, isSuperWallRight);
        }
        else if (!isWallLeft && !isWallRight && isWallUp && isWallDown)
        {
            c = getWallUpDown(isSuperWall, isSuperWallUp, isSuperWallDown);
        }
        else if (!isWallLeft && !isWallRight && isWallUp && !isWallDown)
        {
            c = getWallUp(isSuperWall, isSuperWallUp);
        }
        else if (!isWallLeft && !isWallRight && !isWallUp && isWallDown)
        {
            c = getWallDown(isSuperWall, isSuperWallDown);
        }
        else if (!isWallLeft && !isWallRight && !isWallUp && !isWallDown)
        {
            if (isSuperWall)
            {
                c = DOUBLE_CONNECTOR_UP_DOWN_LEFT_RIGHT;
            }
            else
            {
                c = CONNECTOR_UP_DOWN_LEFT_RIGHT;
            }
        }
        return c;
    }
    
    private static char getWallUp(boolean isSuperWall, boolean isSuperWallUp)
    {
        char c = '?';
        if (isSuperWallUp && isSuperWall)
        {
            c= DOUBLE_V_LINE;
        }
        else
        {
            c = V_LINE;
        }
        return c;
    }

    private static char getWallDown(boolean isSuperWall, boolean isSuperWallDown)
    {
        char c = '?';
        if (isSuperWallDown && isSuperWall)
        {
            c= DOUBLE_V_LINE;
        }
        else
        {
            c = V_LINE;
        }
        return c;
    }

    private static char getWallLeft(boolean isSuperWall, boolean isSuperWallLeft)
    {
        char c = '?';
        if (isSuperWallLeft)
        {
            c = DOUBLE_H_LINE;
        }
        else
        {
            c = H_LINE;
        }
        return c;
    }

    private static char getWallLeftDown(boolean isSuperWall, boolean isSuperWallLeft, boolean isSuperWallDown)
    {
        char c = '?';
        if (isSuperWallLeft && isSuperWallDown)
        {
            c = DOUBLE_CORNER_UP_RIGHT;
        }
        else if (isSuperWallLeft && !isSuperWallDown)
        {
            c = CORNER_UP_RIGHT_DOUBLE_LEFT;
        }
        else if (!isSuperWallLeft && isSuperWallDown)
        {
            if (isSuperWall)
            {
                c = CORNER_UP_RIGHT_DOUBLE_DOWN;
            }
            else
            {
                c = CORNER_UP_RIGHT;
            }
        }
        else if (!isSuperWallLeft && !isSuperWallDown)
        {
            c = CORNER_UP_RIGHT;
        }
        return c;
    }

    private static char getWallLeftUp(boolean isSuperWall, boolean isSuperWallLeft, boolean isSuperWallUp)
    {
        char c = '?';
        if (isSuperWallLeft && isSuperWallUp)
        {
            c = DOUBLE_CORNER_DOWN_RIGHT;
        }
        else if (isSuperWallLeft && !isSuperWallUp)
        {
            c = CORNER_DOWN_RIGHT_DOUBLE_LEFT;
        }
        else if (!isSuperWallLeft && isSuperWallUp)
        {
            c = CORNER_DOWN_RIGHT_DOUBLE_UP;
        }
        else if (!isSuperWallLeft && !isSuperWallUp)
        {
            c = CORNER_DOWN_RIGHT;
        }
        return c;
    }

    private static char getWallLeftUpDown(boolean isSuperWall, boolean isSuperWallLeft, boolean isSuperWallUp, boolean isSuperWallDown)
    {
        char c = '?';
        if (isSuperWallLeft && isSuperWallUp && isSuperWallDown)
        {
            c = DOUBLE_CONNECTOR_UP_DOWN_LEFT;
        }
        else if (isSuperWallLeft && isSuperWallUp && !isSuperWallDown)
        {
            if (isSuperWall)
            {
                c = DOUBLE_CONNECTOR_UP_DOWN_LEFT;
            }
            else
            {
                c = CONNECTOR_UP_DOWN_LEFT;
            }
        }
        else if (isSuperWallLeft && !isSuperWallUp && isSuperWallDown)
        {
            c = DOUBLE_CORNER_UP_RIGHT;
        }
        else if (isSuperWallLeft && !isSuperWallUp && !isSuperWallDown)
        {
            c = CONNECTOR_UP_DOWN_LEFT_DOUBLE_LEFT;
        }
        else if (!isSuperWallLeft && isSuperWallUp && isSuperWallDown)
        {
            c = CONNECTOR_UP_DOWN_LEFT_DOUBLE_UP_DOWN;
        }
        else if (!isSuperWallLeft && isSuperWallUp && !isSuperWallDown)
        {
            c = DOUBLE_CONNECTOR_UP_DOWN_LEFT;
        }
        else if (!isSuperWallLeft && !isSuperWallUp && isSuperWallDown)
        {
            c = DOUBLE_CONNECTOR_UP_DOWN_LEFT;
        }
        else if (!isSuperWallLeft && !isSuperWallUp && !isSuperWallDown)
        {
            c = CONNECTOR_UP_DOWN_LEFT;
        }
        return c;
    }

    private static char getWallLeftRight(boolean isSuperWall, boolean isSuperWallLeft, boolean isSuperWallRight)
    {
        char c = '?';
        if (isSuperWallLeft && isSuperWallRight)
        {
            c = DOUBLE_H_LINE;
        }
        else if (isSuperWallLeft && !isSuperWallRight)
        {
            if (isSuperWall)
            {
                c = DOUBLE_H_LINE;
            }
            else
            {
                c = H_LINE;
            }
        }
        else if (!isSuperWallLeft && isSuperWallRight)
        {
            if (isSuperWall)
            {
                c = DOUBLE_H_LINE;
            }
            else
            {
                c = H_LINE;
            }
        }
        else if (!isSuperWallLeft && !isSuperWallRight)
        {
            c = H_LINE;
        }
        return c;
    }

    private static char getWallLeftRightDown(boolean isSuperWall, boolean isSuperWallLeft, boolean isSuperWallRight, boolean isSuperWallDown)
    {
        char c = '?';
        if (isSuperWallLeft && isSuperWallRight && isSuperWallDown)
        {
            c = DOUBLE_CONNECTOR_LEFT_RIGHT_DOWN;
        }
        else if (isSuperWallLeft && isSuperWallRight && !isSuperWallDown)
        {
            if (isSuperWall)
            {
                c = CONNECTOR_LEFT_RIGHT_DOWN_DOUBLE_LEFT_RIGHT;
            }
            else
            {
                c = CONNECTOR_LEFT_RIGHT_DOWN;
            }
        }
        else if (isSuperWallLeft && !isSuperWallRight && isSuperWallDown)
        {
            c = CONNECTOR_LEFT_RIGHT_DOWN;
        }
        else if (isSuperWallLeft && !isSuperWallRight && !isSuperWallDown)
        {
            c = CORNER_UP_RIGHT_DOUBLE_LEFT;
        }
        else if (!isSuperWallLeft && isSuperWallRight && isSuperWallDown)
        {
            c = CONNECTOR_LEFT_RIGHT_DOWN;
        }
        else if (!isSuperWallLeft && isSuperWallRight && !isSuperWallDown)
        {
            c = CORNER_UP_LEFT_DOUBLE_RIGHT;
        }
        else if (!isSuperWallLeft && !isSuperWallRight && isSuperWallDown)
        {
            c = CONNECTOR_LEFT_RIGHT_DOWN_DOUBLE_DOWN;
        }
        else if (!isSuperWallLeft && !isSuperWallRight && !isSuperWallDown)
        {
            c = CONNECTOR_LEFT_RIGHT_DOWN;
        }
        return c;
    }

    private static char getWallLeftRightUp(boolean isSuperWall, boolean isSuperWallLeft, boolean isSuperWallRight, boolean isSuperWallUp)
    {
        char c = '?';
        if (isSuperWallLeft && isSuperWallRight && isSuperWallUp)
        {
            c = DOUBLE_CONNECTOR_LEFT_RIGHT_UP;
        }
        else if (isSuperWallLeft && isSuperWallRight && !isSuperWallUp)
        {
            c = CONNECTOR_LEFT_RIGHT_UP_DOUBLE_LEFT_RIGHT;
        }
        else if (isSuperWallLeft && !isSuperWallRight && isSuperWallUp)
        {
            c = DOUBLE_CONNECTOR_LEFT_RIGHT_UP;
        }
        else if (isSuperWallLeft && !isSuperWallRight && !isSuperWallUp)
        {
            c = DOUBLE_CONNECTOR_LEFT_RIGHT_UP;
        }
        else if (!isSuperWallLeft && isSuperWallRight && isSuperWallUp)
        {
            c = DOUBLE_CONNECTOR_LEFT_RIGHT_UP;
        }
        else if (!isSuperWallLeft && isSuperWallRight && !isSuperWallUp)
        {
            c = DOUBLE_CONNECTOR_LEFT_RIGHT_UP;
        }
        else if (!isSuperWallLeft && !isSuperWallRight && isSuperWallUp)
        {
            c = CONNECTOR_LEFT_RIGHT_UP_DOUBLE_UP;
        }
        else if (!isSuperWallLeft && !isSuperWallRight && !isSuperWallUp)
        {
            c = CONNECTOR_LEFT_RIGHT_UP;
        }
        return c;
    }

    private static char getWallLeftRightUpDown(boolean isSuperWall, boolean isSuperWallLeft, boolean isSuperWallRight, boolean isSuperWallUp, boolean isSuperWallDown)
    {
        char c = '?';
        if (isSuperWallLeft && isSuperWallRight && isSuperWallUp && isSuperWallDown)
        {
            c = DOUBLE_CONNECTOR_UP_DOWN_LEFT_RIGHT;
        }
        else if (isSuperWallLeft && isSuperWallRight && isSuperWallUp && !isSuperWallDown)
        {
            if (isSuperWall)
            {
                c = DOUBLE_CONNECTOR_LEFT_RIGHT_UP;
            }
            else
            {
                c = CONNECTOR_UP_DOWN_LEFT_RIGHT;
            }
        }
        else if (isSuperWallLeft && isSuperWallRight && !isSuperWallUp && isSuperWallDown)
        {
            c = DOUBLE_CONNECTOR_LEFT_RIGHT_DOWN;
        }
        else if (isSuperWallLeft && isSuperWallRight && !isSuperWallUp && !isSuperWallDown)
        {
            c = CONNECTOR_UP_DOWN_LEFT_RIGHT_DOUBLE_LEFT_RIGHT;
        }
        else if (isSuperWallLeft && !isSuperWallRight && isSuperWallUp && isSuperWallDown)
        {
            if (isSuperWall)
            {
                c = DOUBLE_CONNECTOR_UP_DOWN_LEFT;
            }
            else
            {
                c = CONNECTOR_LEFT_RIGHT_UP;
            }
        }
        else if (isSuperWallLeft && !isSuperWallRight && isSuperWallUp && !isSuperWallDown)
        {
            if (isSuperWall)
            {
                c = DOUBLE_CORNER_DOWN_RIGHT;
            }
            else
            {
                c = CONNECTOR_UP_DOWN_LEFT_RIGHT;
            }
        }
        else if (isSuperWallLeft && !isSuperWallRight && !isSuperWallUp && isSuperWallDown)
        {
            c = DOUBLE_CORNER_UP_RIGHT;
        }
        else if (isSuperWallLeft && !isSuperWallRight && !isSuperWallUp && !isSuperWallDown)
        {
            if (isSuperWall)
            {
                c = DOUBLE_CORNER_UP_RIGHT;
            }
            else
            {
                c = CONNECTOR_UP_DOWN_RIGHT;
            }
        }
        else if (!isSuperWallLeft && isSuperWallRight && isSuperWallUp && isSuperWallDown)
        {
            if (isSuperWall)
            {
                c = DOUBLE_CONNECTOR_UP_DOWN_RIGHT;
            }
            else
            {
                c = CONNECTOR_LEFT_RIGHT_UP;
            }
        }
        else if (!isSuperWallLeft && isSuperWallRight && isSuperWallUp && !isSuperWallDown)
        {
            if (isSuperWall)
            {
                c = DOUBLE_CORNER_DOWN_LEFT;
            }
            else
            {
                c = CONNECTOR_UP_DOWN_LEFT_RIGHT;
            }
        }
        else if (!isSuperWallLeft && isSuperWallRight && !isSuperWallUp && isSuperWallDown)
        {
            c = DOUBLE_CORNER_DOWN_RIGHT;
        }
        else if (!isSuperWallLeft && isSuperWallRight && !isSuperWallUp && !isSuperWallDown)
        {
            if (isSuperWall)
            {
                c = DOUBLE_CORNER_UP_LEFT;
            }
            else
            {
                c = CONNECTOR_UP_DOWN_LEFT;
            }
        }
        else if (!isSuperWallLeft && !isSuperWallRight && isSuperWallUp && isSuperWallDown)
        {
            c = CONNECTOR_LEFT_RIGHT_UP;
        }
        else if (!isSuperWallLeft && !isSuperWallRight && isSuperWallUp && !isSuperWallDown)
        {
            c = CONNECTOR_UP_DOWN_LEFT_RIGHT;
        }
        else if (!isSuperWallLeft && !isSuperWallRight && !isSuperWallUp && isSuperWallDown)
        {
            c = CONNECTOR_LEFT_RIGHT_UP;
        }
        else if (!isSuperWallLeft && !isSuperWallRight && !isSuperWallUp && !isSuperWallDown)
        {
            if (isSuperWall)
            {
                c = DOUBLE_CONNECTOR_LEFT_RIGHT_DOWN;
            }
            else
            {
                c = CONNECTOR_UP_DOWN_LEFT_RIGHT;
            }
        }
        return c;
    }

    private static char getWallRight(boolean isSuperWall, boolean isSuperWallRight)
    {
        char c;
        if (isSuperWallRight)
        {
            c = DOUBLE_H_LINE;
        }
        else
        {
            c = H_LINE;
        }
        return c;
    }
    
    private static char getWallRightDown(boolean isSuperWall, boolean isSuperWallRight, boolean isSuperWallDown)
    {
        char c = '?';
        if (isSuperWallRight && isSuperWallDown)
        {
            if (isSuperWall)
            {
                c = DOUBLE_CORNER_UP_LEFT;
            }
            else
            {
                c = CORNER_UP_LEFT_DOUBLE_RIGHT;
            }
        }
        else if (isSuperWallRight && !isSuperWallDown)
        {
            c = CORNER_UP_LEFT;
        }
        else if (!isSuperWallRight && isSuperWallDown)
        {
            if (isSuperWall)
            {
                c = CORNER_UP_LEFT_DOUBLE_DOWN;
            }
            else
            {
                c = CORNER_UP_LEFT;
            }
        }
        else if (!isSuperWallRight && !isSuperWallDown)
        {
            c = CORNER_UP_LEFT;
        }
        return c;
    }
    
    private static char getWallRightUp(boolean isSuperWall, boolean isSuperWallRight, boolean isSuperWallUp)
    {
        char c = '?';
        if (isSuperWallRight && isSuperWallUp)
        {
            c = DOUBLE_CORNER_DOWN_LEFT;
        }
        else if (isSuperWallRight && !isSuperWallUp)
        {
            c = CORNER_DOWN_LEFT_DOUBLE_RIGHT;
        }
        else if (!isSuperWallRight && isSuperWallUp)
        {
            c = CORNER_DOWN_LEFT_DOUBLE_UP;
        }
        else if (!isSuperWallRight && !isSuperWallUp)
        {
            c = CORNER_DOWN_LEFT;
        }
        return c;
    }
    
    private static char getWallRightUpDown(boolean isSuperWall, boolean isSuperWallRight, boolean isSuperWallUp, boolean isSuperWallDown)
    {
        char c = '?';
        if (isSuperWallRight && isSuperWallUp && isSuperWallDown)
        {
            c = DOUBLE_CONNECTOR_UP_DOWN_RIGHT;
        }
        else if (isSuperWallRight && isSuperWallUp && !isSuperWallDown)
        {
            c = DOUBLE_CONNECTOR_UP_DOWN_RIGHT;
        }
        else if (isSuperWallRight && !isSuperWallUp && isSuperWallDown)
        {
            c = DOUBLE_CORNER_UP_LEFT;
        }
        else if (isSuperWallRight && !isSuperWallUp && !isSuperWallDown)
        {
            c = CONNECTOR_UP_DOWN_RIGHT_DOUBLE_RIGHT;
        }
        else if (!isSuperWallRight && isSuperWallUp && isSuperWallDown)
        {
            c = CONNECTOR_UP_DOWN_RIGHT_DOUBLE_UP_DOWN;
        }
        else if (!isSuperWallRight && isSuperWallUp && !isSuperWallDown)
        {
            c = DOUBLE_CONNECTOR_UP_DOWN_RIGHT;
        }
        else if (!isSuperWallRight && !isSuperWallUp && isSuperWallDown)
        {
            c = DOUBLE_CONNECTOR_UP_DOWN_RIGHT;
        }
        else if (!isSuperWallRight && !isSuperWallUp && !isSuperWallDown)
        {
            c = CONNECTOR_UP_DOWN_RIGHT;
        }
        return c;
    }
    
    private static char getWallUpDown(boolean isSuperWall, boolean isSuperWallUp, boolean isSuperWallDown)
    {
        char c = '?';
        if (isSuperWallUp && isSuperWallDown)
        {
            if (isSuperWall)
            {
                c = DOUBLE_V_LINE;
            }
            else
            {
                c = V_LINE;
            }
        }
        else if (isSuperWallUp && !isSuperWallDown)
        {
            if (isSuperWall)
            {
                c = DOUBLE_V_LINE;
            }
            else
            {
                c = V_LINE;
            }
        }
        else if (!isSuperWallUp && isSuperWallDown)
        {
            if (isSuperWall)
            {
                c = DOUBLE_V_LINE;
            }
            else
            {
                c = V_LINE;
            }
        }
        else if (!isSuperWallUp && !isSuperWallDown)
        {
            c = V_LINE;
        }
        return c;
    }
    
}
